package com.gaohe.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaohe.common.R;
import com.gaohe.mapper.OrderDetailMapper;
import com.gaohe.mapper.OrdersMapper;
import com.gaohe.pojo.OrderDetail;
import com.gaohe.pojo.Orders;
import com.gaohe.pojo.Shop;
import com.gaohe.pojo.User;
import com.gaohe.service.OrderDetailService;
import com.gaohe.service.OrdersService;
import com.gaohe.service.ShopService;
import com.gaohe.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.sf.jsqlparser.statement.select.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.print.attribute.standard.PrinterURI;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders>
        implements OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private ShopService shopService;
    @Autowired
    private OrderDetailService orderDetailService;

    @Override
    public PageInfo<Orders> getAll(Integer page, Integer pageSize, Integer status) {
        //
        PageHelper.startPage(page, pageSize);
        List<Orders> ordersList = ordersMapper.getAll(status);
        PageInfo<Orders> ordersPageInfo = new PageInfo<>(ordersList);
        return ordersPageInfo;
    }

    //订单
    @Override
    @Transactional
    public boolean placeOrder(Orders orders) {
        System.err.println(orders);
        //获取用户 id
        Long userId = orders.getUserId();
        Long shopId = orders.getShopId();
        //根据 id 获取 用户 商品信息

        User user = userService.getById(userId);
        Shop shop = shopService.getById(shopId);
        //判断商品是否停售
        if (shop.getStatus() == 0) {
            return false;
        }
        //创建订单
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setCreateTime(LocalDateTime.now());
        orderDetail.setEndTime(LocalDateTime.now());
        orderDetail.setDescription(user.getName() + "购买了" + shop.getShopName());
        orderDetailService.save(orderDetail);
        orders.setShopName(shop.getShopName());
        orders.setStatus(2);
        orders.setAccount(BigDecimal.valueOf(orders.getShopCount()) .multiply(shop.getNewPrice()) );
        orders.setPhone(user.getPhone());
        orders.setImage(shop.getImage());
        orders.setPrice(shop.getNewPrice());
        orders.setOrderId(orderDetail.getId());
        ordersMapper.insert(orders);
        //修改商品库存
        shop.setInventory(shop.getInventory() - orders.getShopCount());
        shopService.updateById(shop);
        return true;
    }

    //根据 用户 id 查看订单
    @Override
    public List<Orders> getOrdersByUserId(Long userId, String shopName) {
        return ordersMapper.getOrdersByUserId(userId, shopName);
    }

    //删除订单
    @Override
    @Transactional
    public boolean deleteById(Long id) {
        LambdaQueryWrapper<Orders> qw = new LambdaQueryWrapper<>();
        qw.eq(Orders::getId,id);
        Orders orders = this.getOne(qw);
        LambdaQueryWrapper<OrderDetail> qwod = new LambdaQueryWrapper<>();
        qwod.eq(OrderDetail::getId, orders.getOrderId());
        boolean r1 = orderDetailService.remove(qwod);
        boolean r2 = this.removeById(id);
        return r1 && r2 ? true : false;
    }
}
